<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      background-color: #ccc;
    }

    #app {
      width: 400px;
      margin: 20px auto;
      background-color: #fff;
      border: 4px solid blueviolet;
      border-radius: 1em;
      box-shadow: 3px 3px 3px rgba(0, 0, 0, .5);
      padding: 1em 2em 2em;
    }

    h3 {
      text-align: center;
    }

    .title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      border: 1px solid #ccc;
      padding: 0 1em;
    }

    .title h4 {
      line-height: 2;
      margin: 0;
    }

    .container {
      border: 1px solid #ccc;
      padding: 0 1em;
    }

    .btn {
      /* 鼠标改成手的形状 */
      cursor: pointer;
    }

    .router-link-active {
      color: red;
      font-size: 30px;
    }
  </style>
</head>


<body>
  <div id="app">
    <router-view></router-view>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script src="https://cdn.bootcss.com/vue-router/3.1.3/vue-router.min.js"></script>
  <script>
    const router = new VueRouter({
      routes: [
        {
          path: '/login',
          component: {
            name: 'login',
            template: `
            <div>登录页跳转到主页
              <button @click="hLogin">点击登录<button>
              </div>
            `,
            methods: {
              hLogin() {
                //随机数是0到1，假设有50%
                if (Math.random() > 0.5) {
                  // this.$router.push(地址)
                  // $router: 是组件上的一个对象，在它的原型对象上面有很多的方法， push()用来做路由切换。
                  this.$router.push('/index')
                } else {
                  alert('失败')
                }
              }
            }
          }
        },
        { path: '/index', component: { name: 'index', template: `<div>我是主页</div>` } }
      ]
    })
    const vm = new Vue({
      el: '#app',
      router: router
    })
  </script>
</body>

</html>